<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>vue学习</title>
    <script src="./js/vue.js"></script>
  </head>
  <body>
    <div id="app">
      <!-- @click它是在冒泡阶段执行 -->
      <button @click="clickHandle" v-debounce>点击就请求</button>
    </div>
    <script>
      const app = Vue.createApp({
        data() {
          return {
            title: 'abc'
          }
        },
        methods: {
          clickHandle() {
            console.log('clickHandle', this.title)
          }
        },
        directives: {
          debounce: {
            mounted(el, bindings, vnode) {
              let timer = null
              // 给指定绑定的元素添加一个捕获阶段的事件
              el.addEventListener(
                'click',
                (e) => {
                  // 阻止事件链传播
                  e.stopPropagation()
                  // for (let key in vnode.props) {
                  //   // vnode.props[key].call(vnode.props[key])
                  //   vnode.props[key]()
                  // }
                  if (timer) {
                    clearTimeout(timer)
                    timer = null
                  }
                  timer = setTimeout(() => {
                    vnode.props.onClick()
                  }, 1000)
                },
                true
              )
            }
          }
        }
      })
      app.mount('#app')
    </script>
  </body>
</html>
